<?php


namespace App\Models\Admin;


class Order extends Base
{
    const OPERATE_PID_PF = 1;
    const OPERATE_PID = 5;
    const MODEL = 'dispatch_order';

    protected $table = 'order';
    public $timestamps = true;
    protected $dateFormat = 'U';
    protected $guarded = [];

    const ORDER_REVIEW = 0;//订单审核中
    const ORDER_STOCK_UP = 1;//订单备货中
    const ORDER_SHIPPED = 2;//订单已发货
    const ORDER_COMPLETED = 3;//订单已完成
    const ORDER_CANCELLED = 4;//订单已取消

    public function orderList()
    {
        return $this->hasMany(OrderList::class,'oid','id');
    }

    public function member()
    {
        return $this->hasOne(Member::class,'id','uid')->select('id','nickname');
    }

    public function line()
    {
        return $this->hasOne(Line::class,'id','line_id');
    }

    public function salesman()
    {
        return $this->hasOne(SalesMan::class,'id','salesman_id');
    }

    public function companyName()
    {
        return $this->hasOne(Company::class,'id','company_id')->select('id','company_name');
    }

    public function driver()
    {
        return $this->hasOne(Driver::class,'id','driver_id');
    }


    public function seachone($send_time,$ids,$company_name){
        $select=$this->with(['member','driver:id,name','line:id,title','salesman:id,name','companyName']);
        if($send_time){
            $arr = explode(' ', $send_time);
            $select=$select->whereBetween('send_time', [strtotime($arr[0]), strtotime($arr[1]) + (24 * 60 * 60 - 1)]);

        }
        if($ids){
            $select=$select->whereIn('id',$ids);
        }
        if($company_name){
            $select= $select->whereIn('company_uid',Company::where('company_name', 'like', "%$company_name%")->where('model','admin')->get()->pluck('id'));
        }

        $result=$select->orderBy('id','desc')->get()->toArray();
        return $result;
    }
}
